package com.example.springback.controller;

import com.example.springback.common.JWTUtil;
import com.example.springback.common.Result;
import com.example.springback.domain.Admin;
import com.example.springback.domain.Employee;
import com.example.springback.domain.Enterprise;
import com.example.springback.service.AdminService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    //管理员登录
    @PostMapping("/login")
    public Result login( @RequestBody Admin admin){
        Admin admin1=adminService.login(admin);
        // 登录成功，生成 token
        String token = JWTUtil.createToken(admin1.getUsername());
        // 自定义一个返回对象
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("user", admin1);
        return Result.success(result);
    }

    @GetMapping("/searchPage")
    public Result selectPage(Admin admin , @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Admin> adminPageInfo =adminService.selectPage(admin,pageNumber,pageSize);
        return Result.success(adminPageInfo);
    }
    //新增员工
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin){
        adminService.add(admin);
        return Result.success();
    }
    //更新员工信息
    @PostMapping ("/update")
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success();
    }
    //删除员工信息
    @PostMapping ("/delete")
    public Result delete(@RequestBody Admin admin){
        adminService.delete(admin.getAdminId());
        return Result.success();
    }

}
